import random
from collections import defaultdict

row_size = 9
col_size = 10
# accessible range of index [1, row_size-2]
# valid range of index [0, row_size-1]

class Map:
	def getPercept(self,x,y):
		return [x,y,self.map[x][y]]

	def updateLocation(self, dx, dy, do):
		self.robotX += dx
		self.robotY += dy
		self.robotO = do

	def getDirArrayBasedOnOrientation(self):
		return self.d_x[self.robotO], self.d_y[self.robotO]


	def __init__(self, role):
		self.robotX = 1
		self.robotY = 1
		self.robotO = 0 #initial orientation is UP (0)
		self.row_size = row_size
		self.col_size = col_size

		self.d_x = [
			[0, 1, 0, -1],
			[1, 0, -1, 0],
			[0, -1, 0, 1],
			[-1, 0, 1, 0]
		]

		self.d_y = [
			[1, 0, -1, 0],
			[0, -1, 0, 1],
			[-1, 0, 1, 0],
			[0, 1, 0, -1]
		]

		if role == 'robot':
			self.map = [[-2 for x in range(col_size)] for x in range(row_size)]
			self.map[self.robotX][self.robotY] = 1

		if role == 'room':
			# assume 1/6 are obstacles
			self.map = [[random.randint(0, 5) for e in range(col_size)] for e in range(row_size)]
			for i in range(row_size):
				for j in range(col_size):
					if i == 0 or i == row_size-1 or j == 0 or j == col_size-1:
						self.map[i][j] = -1

	def initializeMap(self, index):
		self.map = [[random.randint(0, 5) for e in range(col_size)] for e in range(row_size)]
		for i in range(row_size):
			for j in range(col_size):
				if i == 0 or i == row_size-1 or j == 0 or j == col_size-1:
					self.map[i][j] = -1
				else:
					self.generateroommap(index,i,j)

	def generateroommap(self,index,i,j):
		if index == 1:
			self.map[1][3] = -1
			self.map[1][4] = -1
			self.map[1][5] = -1
			self.map[4][2] = -1
			self.map[7][7] = -1
			self.map[7][6] = -1
			self.map[2][8] = -1
		elif index == 0:
			self.map[i][j] = -1 if self.map[i][j] ==0 else 0

